<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行名称" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入银行名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入证件号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信用卡" prop="creditCard">
        <el-input
          v-model="queryParams.creditCard"
          placeholder="请输入信用卡"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:submission:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:submission:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:submission:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="submissionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属机构" align="center" prop="mechanism" />
      <el-table-column label="银行预留手机号" align="center" prop="phone" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="商户注册省" align="center" prop="provinceName" />
      <el-table-column label="商户注册市" align="center" prop="cityName" />
      <el-table-column label="商户注册区县" align="center" prop="areaName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:submission:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:submission:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户报件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row><h3 style="color: blue;">|法人/个人信息</h3></el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name" >
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证件类型" prop="idCardType" >
              <el-select v-model="form.idCardType" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.idCard_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="证件号" prop="idCard" >
              <el-input v-model="form.idCard" placeholder="请输入证件号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期开始日期" prop="idCardBegin" >
              <el-input v-model="form.idCardBegin" placeholder="请输入证件号有效期开始日期" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
        <el-col :span="12">
          <el-form-item label="有效期结束日期" prop="idCardEnd" >
            <el-input v-model="form.idCardEnd" placeholder="请输入证件号有效期结束日期" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商户注册省份" prop="regionProvince" >
            <el-select v-model="form.regionProvince" placeholder="请选择省份">
              <el-option
                v-for="item in provinceOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                @click.native="getRegionChildTwo(item)">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="商户注册城市" prop="regionCity" >
              <el-select v-model="form.regionCity" placeholder="请选择城市">
                <el-option
                  v-for="item in cityOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                  @click.native="getRegionChildThree(item)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商户注册区县" prop="regionArea" >
              <el-select v-model="form.regionArea" placeholder="请选择区县">
                <el-option
                  v-for="item in areaOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
        <el-col :span="12">
          <el-form-item label="所属机构" prop="mechanism" >
            <el-select v-model="form.mechanismName" placeholder="请选择所属机构">
              <el-option :value="form.mechanism" style="height: auto">
                <el-tree
                  :data="data"
                  node-key="id"
                  ref="tree"
                  highlight-current
                  @node-click="handleNodeClick"
                  :props="defaultProps">
                </el-tree>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行预留手机号" prop="phone" >
            <el-input v-model="form.phone" placeholder="请输入银行预留手机号" />
          </el-form-item>
        </el-col>
      </el-row>
        <el-row><h3 style="color: blue;">|补充信息（非必填）</h3></el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="信用卡" prop="creditCard">
              <el-input v-model="form.creditCard" placeholder="请输入信用卡" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信用卡有效期" prop="creditCardValidity">
              <el-input v-model="form.creditCardValidity" placeholder="请输入信用卡有效期" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row><h3 style="color: blue;">|费率信息</h3></el-row>
        <el-form-item label="终端费率" prop="terminationRate" >
          <el-input v-model="form.terminationRate" placeholder="请输入终端费率" />
        </el-form-item>
        <el-row><h3 style="color: blue;">|结算信息</h3></el-row>
        <el-form-item label="银行账号" prop="bankCard" >
          <el-input v-model="form.bankCard" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="银行编码" prop="bankCode" >
          <el-input v-model="form.bankCode" placeholder="请输入银行编码" />
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName" >
          <el-input v-model="form.bankName" placeholder="请输入银行名称" />
        </el-form-item>
        <el-row><h3 style="color: blue;">|上传商户图片</h3></el-row>
        <el-form-item label="上传附件">
          <el-upload
            :file-list="fileList"
            action="#"
            ref="uploadimg"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :data="form"
            :limit="2"
            multiple
            :http-request="(file)=>{return httpRequest(file)}"
            :auto-upload="false">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="imgPath" alt="">
          </el-dialog>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSubmission, getSubmission, delSubmission, addSubmission, updateSubmission,getRegionRoot,getRegionChild,uploadImg } from "@/api/posp/merchantSubmission";
import { buildUserTree } from "@/api/system/user";

export default {
  name: "Submission",
  dicts: ['idCard_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户报件表格数据
      submissionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      provinceOptions: {},
      cityOptions:{},
      areaOptions:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mechanism: null,
        phone: null,
        name: null,
        bankName: null,
        idCard: null,
        creditCard: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      dialogVisible: false,
      files:[],
      fileList:[],
      data: [],
      defaultProps: {
        children: "children",
        label: "nickName"
      },
      imgPath: null
    };
  },
  created() {
    this.getList();
    this.getRegionRoot("1");
    this.optionData();
  },
  methods: {
    /* 转树形数据 */
    optionData() {
      buildUserTree().then(response => {
        this.data = response;
      });
    },
    handleNodeClick(data) {
      this.form.mechanismName = data.nickName
      this.form.mechanism = data.userId
    },
    httpRequest(param){
      this.files.push( param.file);
    },
    getRegionRoot(level) {
      getRegionRoot(level).then(response => {
        this.provinceOptions = response.data;
      });
    },
    getRegionChildTwo(item){
      let pId = item.id
      getRegionChild(pId).then(response => {
        this.cityOptions = response.data;
        this.form.regionCity = null
        this.form.regionArea = null
      });
    },
    getRegionChildThree(item){
      let pId = item.id
      getRegionChild(pId).then(response => {
        this.areaOptions = response.data;
        this.form.regionArea = null
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.imgPath = file.url;
      this.dialogVisible = true;
    },
    /** 查询商户报件列表 */
    getList() {
      this.loading = true;
      listSubmission(this.queryParams).then(response => {
        this.submissionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phone: null,
        name: null,
        regionProvince: null,
        regionCity: null,
        regionArea: null,
        terminationRate: null,
        bankCard: null,
        bankCode: null,
        bankName: null,
        idCard: null,
        idCardBegin: null,
        idCardEnd: null,
        idCardType: null,
        creditCardValidity: null,
        creditCard: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        filePaths:[],
        mechanismName : null,
        mechanism: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.fileList = [];
      this.open = true;
      this.title = "添加商户报件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRegionChild(row.regionProvince).then(response => {
        this.cityOptions = response.data;
      });
      getRegionChild(row.regionCity).then(response => {
        this.areaOptions = response.data;
      });
      getSubmission(id).then(response => {
        this.form = response.data;
        if(this.form.fileBacks){
          let objArr = [];
          this.form.fileBacks.forEach(function (fileBack) {
            let obj = {url:process.env.VUE_APP_BASE_API + fileBack.filePath};
            objArr.push(obj);
          })
          this.fileList = objArr;
        }
        this.open = true;
        this.title = "修改商户报件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$refs.uploadimg.submit();
          //上传类型
          let formData = new FormData();
          this.files.forEach(function (file) {
            formData.append('file', file);
          })
          uploadImg(formData).then( res =>{
            this.files = []
            this.form.filePaths = res.data
            if (this.form.id != null) {
              updateSubmission(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSubmission(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          })
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除该数据项？').then(function() {
        return delSubmission(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
